package com.zlx.guguanjia.controller;

import com.baidu.ueditor.ActionEnter;
import com.zlx.guguanjia.entity.Qualification;
import com.zlx.guguanjia.util.FTPUtil;
import org.apache.commons.net.ftp.FTPClient;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class UeditorController {
    //临时上传路径
    @Value("${uploadPath}")
    String uploadPath;
    //静态路径
    @Value("${staticPath}")
    String staticPath;
    //ftp工具类
    @Autowired
    FTPUtil ftpUtil;
    //ftp主机名
    @Value("${ftpHost}")
    String ftpHost;
    //ftp端口号
    @Value("${ftpPort}")
    int ftpPort;
    //用户名
    @Value("${ftpUsername}")
    String ftpUsername;
    //密码
    @Value("${ftpPassword}")
    String ftpPassword;
    //nginx图片路径
    @Value("${ftpUploadPath}")
    String ftpUploadPath;

    /**
     * 富文本统一请求接口
     */
    @RequestMapping("doUeditor")
    public String doUeditor(String action, HttpServletRequest request, MultipartFile upfile){
        String exec=null;
        if("config".equals(action)){
            exec = new ActionEnter( request, request.getServletContext().getRealPath("/") ).exec();
        }else if("uploadimage".equals(action)){
//            System.out.println("统一请求接口返回图片上传操作结果统一响应对象");
//            System.out.println(upfile);
            //获取图片名字和后缀
            String fileName = upfile.getOriginalFilename();
            //获取图片后缀
            String type = fileName.substring(fileName.lastIndexOf("."));
            //构建临时上次图片文件的文件对象
            File file = new File(uploadPath, fileName);
            try {
                //存储图片到临时路径
                upfile.transferTo(file);
                //获取对应的ftp对象
                FTPClient ftp = ftpUtil.getFTPClient(ftpHost, ftpPort, ftpUsername,ftpPassword);
                //ftp对象进行图片上传
                ftpUtil.uploadFile(ftp, file.getAbsolutePath(), ftpUploadPath);
                //关闭ftp流
                ftpUtil.closeFTP(ftp);
                //把上传成功的数据转换为JSON
                exec = new JSONObject(resultMap("SUCCESS",fileName,upfile.getSize(),fileName,type,staticPath+fileName)).toString();
            } catch (IOException e) {
                e.printStackTrace();
                //把上传失败的数据转换为JSON
                exec = new JSONObject(resultMap("FAIL",null,0,null,null,null)).toString();
            }
        }
        return exec;
    }
    private Map<String,Object> resultMap(String state, String original, long size, String title,String type,  String url){
        Map<String ,Object> result = new HashMap<>();
        //上传图片状态
        result.put("state",state);
        result.put("original",original);
        //文件大小
        result.put("size",size);
        //文件名字
        result.put("title",title);
        //文件类型
        result.put("type",type);
        //文件路径
        result.put("url", url);
        return result;
    }
}
